﻿using System;
using System.Linq;
using System.Text;

namespace Common.Library.StringExt.Randoms
{
    /// <summary>
    /// 随机数扩展
    /// </summary>
    public class RandomExt
    {
        #region 获取到指定长度的随机字符串    不带汉字
        private static char[] constant =
        {
            '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'
        };

        public static string GenerateRandomString(int Length)
        {
            StringBuilder newRandom = new StringBuilder(62);
            Random rd = new Random();
            for (int i = 0; i < Length; i++)
            {
                newRandom.Append(constant[rd.Next(62)]);
            }
            return newRandom.ToString();
        }
        #endregion

        #region 获取随机数组
        public int getNum(int[] arrNum, int tmp, int minValue, int maxValue, Random ra)
        {
            int n = 0;
            while (n <= arrNum.Length - 1)
            {
                if (arrNum[n] == tmp) //利用循环判断是否有重复
                {
                    tmp = ra.Next(minValue, maxValue); //重新随机获取。
                    getNum(arrNum, tmp, minValue, maxValue, ra);//递归:如果取出来的数字和已取得的数字有重复就重新随机获取。
                }
                n++;
            }
            return tmp;
        }

        public int[] getRandomNum(int num, int minValue, int maxValue)
        {
            Random ra = new Random(unchecked((int)DateTime.Now.Ticks));
            int[] arrNum = new int[num];
            int tmp = 0;
            for (int i = 0; i <= num - 1; i++)
            {
                tmp = ra.Next(minValue, maxValue); //随机取数
                arrNum[i] = getNum(arrNum, tmp, minValue, maxValue, ra); //取出值赋到数组中
            }
            return arrNum;
        }
        #endregion

        #region 随机生成一个指定大小的数字
        /// <summary>
        /// 随机生成一个指定大小的数字
        /// </summary>
        /// <param name="min">随机数字的下限，包含该下限</param>
        /// <param name="max">随机数字的上限，不包含上限</param>
        /// <returns></returns>
        public static int GetNumber(int min, int max)
        {
            //种子
            int seed = 0;
            string guid = Guid.NewGuid().ToString();
            // 将字符串转换为字符数组
            char[] charBuf = guid.ToArray();
            ASCIIEncoding charToASCII = new ASCIIEncoding();
            //定义发送缓冲区
            byte[] TxdBuf = new byte[charBuf.Length];
            TxdBuf = charToASCII.GetBytes(charBuf);
            for (int i = 0; i < TxdBuf.Length; i++)
            {
                seed += TxdBuf[i];
            }
            Random random = new Random(seed);
            return random.Next(min, max);
        }
        #endregion

        #region 随机生成一个指定范围内的 decimal 类型的数值
        public static decimal GetDecimal(int min, int max)
        {
            int number = GetNumber(min * 100, max * 100);
            return (decimal)number / 100;
        }
        #endregion
    }
    
}
